package org.iteam.xlz.manager.action.terminal;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.iteam.web.shiro.ShiroUtil;
import org.iteam.web.struts.action.AbstractModuleAction;
import org.iteam.web.struts.action.RestAction;
import org.iteam.xlz.core.entity.base.Area;
import org.iteam.xlz.core.entity.terminal.Terminal;
import org.iteam.xlz.core.service.base.AreaService;
import org.iteam.xlz.core.service.base.AreaService.AreaTree;
import org.iteam.xlz.core.service.product.ProductService;
import org.iteam.xlz.core.service.base.TerminalTypeService;
import org.iteam.xlz.core.service.terminal.TerminalProductService;
import org.iteam.xlz.core.service.terminal.TerminalService;

import net.sf.json.JSONArray;

@SuppressWarnings("unchecked")
@Results({@Result(name="INDEX",type="freemarker",location="/WEB-INF/content/terminal/index.html"),
	      @Result(name="EDIT",type="freemarker",location="/WEB-INF/content/terminal/edit.html"),
	      @Result(name="UP",type="freemarker",location="/WEB-INF/content/terminal/up.html")})
public class TerminalAction extends AbstractModuleAction<Terminal> implements RestAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -109969873064753128L;
	final String PATH="/terminal";
	
	@Override
	public Terminal getModel() {
		if(null==model){
			model=new Terminal();
		}
		return model;
	}
	@Override
	@Action(PATH+"/index")
	public String index() throws Exception {
		AreaService.getInstance().buildAreaTree();
		map.put("areaTree",AreaService.getInstance().getList());
		return INDEX;
	}

	@Override
	@Action(PATH+"/query")
	public String query() {
		map.put("total",TerminalService.getInstance().counts(aid1,aid2,aid3,aid4));
		map.put("rows",TerminalService.getInstance().query(aid1,aid2,aid3,aid4,page,rows));
		return MAP;
	}

	@Override
	public String show() {
		return null;
	}

	@Override
	@Action(PATH+"/create")
	public String create() {
		model.setCreator(ShiroUtil.currentAccount());
		map.put("state",TerminalService.getInstance().create(model));
		return MAP;
	}

	@Override
	@Action(PATH+"/update")
	public String update() {
		map.put("state",TerminalService.getInstance().update(model));
		return MAP;
	}

	@Override
	@Action(PATH+"/product/{_id}/up")
	public String delete() {
		map.put("products",ProductService.getInstance().getOnTerminalProduct(_id));
		return "UP";
	}

	@Override
	@Action(PATH+"/product/{_id}/create")
	public String destroy() {
		map.put("state",TerminalProductService.getInstance().create(pid,_id,productamount,ShiroUtil.currentAccount()));
		return MAP;
	}

	@Override
	@Action(PATH+"/{_id}/edit")
	public String edit()throws Exception {
		Terminal t=TerminalService.getInstance().findById(_id);
		super.object=t;
		map.put("typeTree",TerminalTypeService.getInstance().buildTerminalTree());
		map.put("typeparent",TerminalTypeService.getInstance().getTypeParent(_id));
		
		AreaService.getInstance().buildAreaTree();
		
		Area fourthArea=AreaService.getInstance().findById(t.getAid());
		for(AreaTree at:AreaService.getInstance().getThirdList()){
			if(fourthArea.getPid().equals(at.getId())){
				JSONArray ja=JSONArray.fromCollection(at.getChildren());
				map.put("fourthData",ja.toString());
				map.put("aid3",at.getId());
			
				Area thirdArea=AreaService.getInstance().findById(at.getId());
				for(AreaTree ati:AreaService.getInstance().getSecondList()){
					if(thirdArea.getPid().equals(ati.getId())){
						ja=JSONArray.fromCollection(ati.getChildren());
						map.put("thirdData",ja.toString());
						map.put("aid2",ati.getId());
						
						Area secondArea=AreaService.getInstance().findById(ati.getId());
						for(AreaTree atie:AreaService.getInstance().getList()){
							if(secondArea.getPid().equals(atie.getId())){
								ja=JSONArray.fromCollection(atie.getChildren());
								map.put("secondData",ja.toString());
								map.put("aid1",atie.getId());
								break;
							}
						}
						
						break;
					}
				}
				
				break;
			}
		}
		
		map.put("areaTree",AreaService.getInstance().getList());
		return EDIT;
	}

	@Override
	@Action(PATH+"/new")
	public String editNew() throws Exception {
		map.put("typeTree",TerminalTypeService.getInstance().buildTerminalTree());
		map.put("typeparent",TerminalTypeService.getInstance().getTypeParent(null));
		AreaService.getInstance().buildAreaTree();
		map.put("areaTree",AreaService.getInstance().getList());
		return EDIT;
	}
	
	private String aid1;
	private String aid2;
	private String aid3;
	private String aid4;
	private String pid;
	private String productamount;

	public void setAid1(String aid1) {
		this.aid1 = aid1;
	}
	public void setAid2(String aid2) {
		this.aid2 = aid2;
	}
	public void setAid3(String aid3) {
		this.aid3 = aid3;
	}
	public void setAid4(String aid4) {
		this.aid4 = aid4;
	}
	public void setPid(String pid) {
		this.pid = pid;
	}
	public void setProductamount(String productamount) {
		this.productamount = productamount;
	}
}
